package com.example.concurrent.chapter04;

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.currentThread;

/**
 * @author Create By Liuqz
 * @date 2018/11/21 11:26
 */
public class ThisMonitor {

    public synchronized void method1() {
        System.out.println(currentThread().getName() + " enter to method1");
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void method2() {
        synchronized (this) {
            System.out.println(currentThread().getName() + " enter to method2");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        ThisMonitor monitor = new ThisMonitor();
        new Thread(monitor::method1,"T1").start();
        new Thread(monitor::method2,"T2").start();
    }
}
